
#import "UserTorrentListViewController.h"
#import "UserTorrentDetailsViewController.h"

@implementation UserTorrentListViewController

//
// torrent list
//
@synthesize torrentList														= _torrentList;

//
// type
//
@synthesize type															= _type;

//
// user id
//
@synthesize userId															= _userId;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup refresh button
	//
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTorrentList:)] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.type																= nil;
	self.torrentList														= nil;
	
	[super dealloc];
}

//
// refresh torrent list
//
- (void)refreshTorrentList:(id)sender
{
	//
	// get list
	//
	NexusClient* client														= [NexusClient sharedClient];
	id getUserTorrentListFinished = ^void(GetUserTorrentListRequest* httpRequest)
	{
		[ActivityView removeView];
		if(!httpRequest.error && httpRequest.response.statusCode == 200)
		{
			self.torrentList												= httpRequest.torrentList;
			[self.tableView reloadData];
		}
		else
		{
			NSString* message												= [httpRequest errorDescription];
			NSString* title													= NSLocalizedStringFromTable(@"Get Torrent List Failed", @"UserTorrentListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", @"ok")];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting Torrent List", @"UserTorrentListViewController", nil) networking:YES];
	[client getUserTorrentList:self.userId type:[self.type lowercaseString] callback:getUserTorrentListFinished];
}

//
// did load
//
- (void)viewDidLoad
{
	[super viewDidLoad];
	self.title																= NSLocalizedStringFromTable(([NSString stringWithFormat:@"%@ List", self.type]), @"UserTorrentListViewController", nil);
	[self refreshTorrentList:nil];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.torrentList.count;
}

//
// get footer
//
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
{
	return self.torrentList.count ? [NSString stringWithFormat:(NSLocalizedStringFromTable(@"Total: %d", @"UserTorrentListViewController", nil)), self.torrentList.count] : NSLocalizedStringFromTable(@"None", @"UserTorrentListViewController", nil);
}

//
// build detail info
//
- (NSString*)buildDetailsInfo:(UserTorrentInfo*)info
{
	if([self.type isEqualToString:@"Uploaded"] || [self.type isEqualToString:@"Completed"])
		return [NSString stringWithFormat:NSLocalizedStringFromTable(@"[%@]@[%@]", @"UserTorrentListViewController", nil), info.uploadedSize, info.seedTime];
	
	if([self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"] || [self.type isEqualToString:@"Incomplete"])
		return [NSString stringWithFormat:NSLocalizedStringFromTable(@"[%@]/[%@] %@", @"UserTorrentListViewController", nil), info.uploadedSize, info.downloadedSize, info.shareRatio];
		
	return nil;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:@"s>"];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"s>"] autorelease];
	
	UserTorrentInfo* torrentInfo											= [self.torrentList objectAtIndex:indexPath.row];
	theCell.textLabel.text													= torrentInfo.title;
	theCell.detailTextLabel.text											= [self buildDetailsInfo:torrentInfo];
	theCell.accessoryType													= UITableViewCellAccessoryDisclosureIndicator;
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	UserTorrentDetailsViewController* userTorrentDetailsViewController		= [[[UserTorrentDetailsViewController alloc] init] autorelease];
	userTorrentDetailsViewController.torrentInfo							= [self.torrentList objectAtIndex:indexPath.row];
	userTorrentDetailsViewController.type									= self.type;
	[self.navigationController pushViewController:userTorrentDetailsViewController animated:YES];
}

@end
